package mysql

import (
	"errors"

	"github.com/go-sql-driver/mysql"
)

// 1049	Can't connect to MySQL server on 'host'	主机地址/端口错误、SSH 隧道配置问题、服务未运行
// 1045	Access denied for user	用户名/密码错误、权限不足
// 2002	Can't connect to local MySQL server through socket	Unix Socket 文件路径错误
// 1064	You have an error in your SQL syntax	SQL 语法错误（如缺少分号、引号不匹配）
// 1071	Too many columns specified	INSERT 语句中列数超出表定义
// 1146	Table 'database.table' doesn't exist	表名或数据库名错误
// 1062	Duplicate entry for key 'index'	插入重复主键/唯一索引值
// 1048	Field 'column' cannot be null	尝试向非空字段插入 NULL 值
// 1452	Cannot add or update a child row: foreign key constraint fails	外键约束冲突
// 1205	Lock wait timeout exceeded	事务等待锁超时
// 1213	Deadlock found when trying to acquire lock	事务死锁
// 1044	Access denied for user	用户无权访问特定数据库/表
// 1143	SELECT command denied to user	用户无权执行 SELECT 操作

const (
	TableAlreadyExists1050 uint16 = 1050
	TableNotFind1051       uint16 = 1051
	TableNotFind1146       uint16 = 1146
)

func IsErrors(err error, codes ...uint16) bool {
	// 类型断言到 *mysql.MySQLError (MySQL)
	var mysqlErr *mysql.MySQLError
	if errors.As(err, &mysqlErr) {
		for _, code := range codes {
			if mysqlErr.Number == code {
				return true
			}
		}
	}
	return false
}
